package cn.ztydata.teachingsystem.heaven.dao.redis;

/**
 * Redis键生成工具类
 * Created by chenxin on 2014/12/8.
 */
public class KeyUtils {
    private static final String NAMESPACE = "teachingSystem:";

    /**
     * 课程->知识点
     */
    static String courseKp(final long courseId) {
        return NAMESPACE + "course_kp:" + courseId;
    }

    /**
     * 知识点->课程
     */
    static String kpCourse(final long kpId) {
        return NAMESPACE + "kp_course:" + kpId;
    }

    /**
     * 资源->知识点
     * @param resourceId
     * @return String
     */
    static String resourceKp(final long resourceId){
        return NAMESPACE + "resource_kp:" + resourceId;
    }

    /**
     * 知识点->资源
     * @param kpId
     * @return String
     */
    static String kpResource(final long kpId){
        return NAMESPACE + "kp_resource:" + kpId;
    }

    /**
     * 专业下资源数量统计
     * @param specialtyId 专业
     * @return String
     */
    static String specResourceCount(final long specialtyId){
        return NAMESPACE + "specResourceCount:" + specialtyId;
    }

    /**
     * 资源类型下资源数量统计
     * @param typeId 资源类型编号
     * @return String
     */
    static String typeResourceCount(final long typeId){
        return NAMESPACE + "typeResourceCount:" + typeId;
    }

    /**
     * 章节->知识点
     */
    static String chapterKp(final long chapterId) {
        return NAMESPACE + "chapter_kp:" + chapterId;
    }

    /**
     * 知识点->章节
     */
    static String kpChapter(final long kpId) {
        return NAMESPACE + "kp_chapter:" + kpId;
    }

    /**
     * 章节->资源
     */
    static String chapterRsc(final long chapterId) {
        return NAMESPACE + "chapter_rsc:" + chapterId;
    }

    /**
     * 资源->章节
     */
    static String rscChapter(final long rscId) {
        return NAMESPACE + "rsc_chapter:" + rscId;
    }

    /**
     * 章节->子集
     */
    static String chapterSon(final long nodeId) {
        return NAMESPACE + "chapter:p_s:" + nodeId;
    }

    /**
     * 媒体类型下资源数量统计
     * @param mediaType 媒体类型编号
     * @return String
     */
    static String mediaTypeResourceCount(final long mediaType){
        return NAMESPACE + "mediaTypeResourceCount:" + mediaType;
    }

    /**
     * 资源浏览量统计
     * @param resourceId 资源编号
     * @return String
     */
    static String resourcePV(final long resourceId){
        return NAMESPACE + "resource:pv:" + resourceId;
    }

    /**
     * 群组资源浏览量统计
     * @param groupType 群组类型
     * @param groupId 群组ID
     * @return String
     * @author lyj
     * @since 2015-4-1
     */
    static String groupRscPv(final int groupType, final long groupId){
        return NAMESPACE + "resource:" + groupType + ":" + groupId + ":UsedTimes:";
    }

    /**
     * 资源下载量统计
     * @param resourceId 资源编号
     * @return String
     */
    static String resourceDownloadCount(final long resourceId){
        return NAMESPACE + "resource:downloadCount:" + resourceId;
    }

    /**
     * 生成Redis Key字符串
     * @param key 标识字符
     * @param masterIds 关系主键ID集合
     *
     * @author chenxin
     * @since 2015/2/4
     */
    static String generateKey(final String key, final long... masterIds) {
        StringBuilder result = new StringBuilder(NAMESPACE).append(key);

        for (long masterId : masterIds) {
            result.append(":").append(masterId);
        }

        return result.toString();
    }

    /**
     * 教师收藏->资源
     */
    static String teacherRsc(final long teacherId, final int groupType, final long groupId) {
        return NAMESPACE + "teacher_rsc:" + teacherId + ":" + groupType + ":" + groupId + ":";
    }

    /**
     * 学生收藏->资源
     */
    static String studentRsc(final long studentId, final int groupType, final long groupId) {
        return NAMESPACE + "student_rsc:" + studentId + ":" + groupType + ":" + groupId + ":";
    }

    /**
     * 收藏资源的教师列表
     */
    static String rscTeacher(final long resourceId, final int groupType, final long groupId) {
        return NAMESPACE + "rsc_teacher:" + resourceId + ":" + groupType + ":" + groupId + ":";
    }

    /**
     * 收藏资源的学生列表
     */
    static String rscStudent(final long resourceId,final int groupType, final long groupId){
        return NAMESPACE + "rsc_student:" + resourceId + ":" + groupType + ":" + groupId + ":";
    }

    /**
     * 作业或测试考点
     * @param testId 作业或测试编号
     * @return String
     */
    static String testKp(final long testId){
        return NAMESPACE + "test_kp" + testId;
    }
}
